/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AdminAccessFrame.java
 *
 * Created on Jan 20, 2010, 1:55:59 PM
 */

package org.acmwustl.bubble.gui;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SwingConstants;
import javax.swing.SwingWorker;
import javax.swing.WindowConstants;
import org.acmwustl.bubble.gui.dialogs.NotAnOfficerDialog;
import org.acmwustl.bubble.logic.ConnectionManager;
import org.acmwustl.bubble.windowutils.WindowUtils;

/**
 *
 * @author Tyler
 */
public class AdminAccessFrame extends javax.swing.JFrame
implements KeyListener {

    String keyIDCode = "";
    MainWindow parent;

    private boolean ignoreInput = false;
    
    /** Creates new form AdminAccessFrame */
    public AdminAccessFrame(MainWindow mw) {
        initComponents();
        parent = mw;

        setSize(new Dimension(1024,768));
        WindowUtils.centerWindow(this);

        getRootPane().addKeyListener(this);
        getRootPane().requestFocus();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new JLabel();
        jLabel2 = new JLabel();
        jLabel3 = new JLabel();
        jButton1 = new JButton();

        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setUndecorated(true);

        jLabel1.setFont(new Font("Tahoma", 0, 36));
        jLabel1.setHorizontalAlignment(SwingConstants.CENTER);
        jLabel1.setText("Administration");

        jLabel2.setFont(new Font("Tahoma", 0, 24));
        jLabel2.setHorizontalAlignment(SwingConstants.CENTER);
        jLabel2.setText("Verification Required: Officers Only");

        jLabel3.setFont(new Font("Tahoma", 0, 48));
        jLabel3.setHorizontalAlignment(SwingConstants.CENTER);
        jLabel3.setText("Swipe Card Now");

        jButton1.setFont(new Font("Tahoma", 0, 36));
        jButton1.setText("Cancel");
        jButton1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(Alignment.CENTER)
                    .addComponent(jButton1, GroupLayout.PREFERRED_SIZE, 279, GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3, GroupLayout.DEFAULT_SIZE, 815, Short.MAX_VALUE))
                .addContainerGap())
            .addGroup(Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(10, Short.MAX_VALUE)
                .addComponent(jLabel1, GroupLayout.DEFAULT_SIZE, 815, Short.MAX_VALUE)
                .addGap(10, 10, 10))
            .addGroup(Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(10, Short.MAX_VALUE)
                .addComponent(jLabel2, GroupLayout.DEFAULT_SIZE, 815, Short.MAX_VALUE)
                .addGap(10, 10, 10))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(ComponentPlacement.RELATED)
                .addComponent(jLabel2, GroupLayout.PREFERRED_SIZE, 55, GroupLayout.PREFERRED_SIZE)
                .addGap(122, 122, 122)
                .addComponent(jLabel3, GroupLayout.PREFERRED_SIZE, 80, GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(ComponentPlacement.RELATED, 37, Short.MAX_VALUE)
                .addComponent(jButton1, GroupLayout.PREFERRED_SIZE, 109, GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    public void keyTyped(KeyEvent e) {
        if(!ignoreInput) {
            keyIDCode += e.getKeyChar();
            if(keyIDCode.length() == 19) {
                if(keyIDCode.matches(";\\d{5}=\\d{11}\\?")) {
                    if(ConnectionManager.getInstance().isOfficer(keyIDCode)) {
                        AdminFrame af = new AdminFrame(parent);
                        af.setVisible(true);
                        dispose();
                    }
                    else {
                        new NotAnOfficerDialog(this, true).setVisible(true);
                    }
                }
                else {
                    keyIDCode = "";
                    new IgnoreInputTimer().execute();
                    Toolkit.getDefaultToolkit().beep();
                }
            }
        }
    }

    public void keyPressed(KeyEvent e) {}

    public void keyReleased(KeyEvent e) {}

    private class IgnoreInputTimer extends SwingWorker {

        private long time = 300;
        private boolean running = true;
        private long lastMillis;
        
        public IgnoreInputTimer() {
            ignoreInput = true;
        }

        @Override
        protected Object doInBackground() throws Exception {
            lastMillis = System.currentTimeMillis();
            while(running) {
                time -= (System.currentTimeMillis() - lastMillis);
                lastMillis = System.currentTimeMillis();
                if(time <= 0) {
                    ignoreInput = false;
                    running = false;
                    System.err.println("DONE!");
                }
                else
                    Thread.sleep(100);
            }
            return null;
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private JButton jButton1;
    private JLabel jLabel1;
    private JLabel jLabel2;
    private JLabel jLabel3;
    // End of variables declaration//GEN-END:variables

}
